// pages/position/position.js
import request from '../../utils/request';

Page({
    data: {
        positionList: [], // 修改为 positionList
        originalPositionList: [], // 修改为 originalPositionList
        searchKeyword: '',
        noResults: false
    },
    onLoad() {
        this.fetchPositionList(); // 修改为 fetchPositionList
    },
    // 获取岗位列表
    fetchPositionList() { // 修改为 fetchPositionList
        wx.showLoading({
            title: '正在加载岗位数据...',
        });
        wx.request({
            url: 'http://localhost:8090/xh/position/list',
            method: 'GET',
            header: {
                'Content-Type': 'application/json'
            },
            success: (res) => {
                wx.hideLoading();
                if (res.data.code === 200) {
                    this.setData({
                        positionList: res.data.data, // 修改为 positionList
                        originalPositionList: res.data.data // 修改为 originalPositionList
                    });
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none'
                    });
                }
            },
            fail: () => {
                wx.hideLoading();
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
            }
        });
    },
    // 搜索输入事件处理函数
    searchInput(e) {
        const keyword = e.detail.value.trim();
        this.setData({
            searchKeyword: keyword
        });
        this.filterPositionList(keyword); // 修改为 filterPositionList
    },
    // 模糊搜索岗位列表
    filterPositionList(keyword) { // 修改为 filterPositionList
        if (keyword === '') {
            this.setData({
                positionList: this.data.originalPositionList, // 修改为 positionList
                noResults: false
            });
        } else {
            const lowerCaseKeyword = keyword.toLowerCase();
            const filteredList = this.data.originalPositionList.filter(position => { // 修改为 position
                const title = position.title ? position.title.toLowerCase() : '';
                const company = position.company ? position.company.toLowerCase() : '';
                return title.includes(lowerCaseKeyword) || company.includes(lowerCaseKeyword);
            });
            this.setData({
                positionList: filteredList, // 修改为 positionList
                noResults: filteredList.length === 0
            });
        }
    },
    // 点击岗位进入详情页面
    goToPositionDetail(e) { // 修改为 goToPositionDetail
        const position = e.currentTarget.dataset.position; // 修改为 position
        wx.navigateTo({
            url: `/pages/position/detail/detail?position=${JSON.stringify(position)}` // 修改为 position
        });
    }
});

